package de.vibes.model;

import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.configuration.ConfigurationException;
import org.neodatis.odb.ODB;
import org.neodatis.odb.ODBFactory;
import org.neodatis.odb.ODBRuntimeException;
import org.neodatis.odb.core.query.IQuery;
import org.neodatis.odb.core.query.criteria.Where;
import org.neodatis.odb.impl.core.query.criteria.CriteriaQuery;

import de.vibes.controller.Configuration;

public class Person extends AbstractObject{

	private String username;
	private String password;
	
	private String firstname;
	private String lastname;
	private Date birthday;
	
	private ArrayList<Vibe> vibes = new ArrayList<Vibe>();

	public Person(String username, String password) throws ConfigurationException {
		super();
		this.setUsername(username);
		this.setPassword(password);
	}
	
	/*******************************************************************************
	 * Diese Methode sucht im Datenbank nach Person und gibt die Zurück. Wenn es
	 * keine Person gefunden werden kann wird null zurückgegeben.
	 * 
	 * @param username
	 *            - null oder String
	 * @return
	 * @throws ConfigurationException 
	 *******************************************************************************/
	public static Person findPerson(String username) throws ConfigurationException, ODBRuntimeException {
		if (username == null) {
			return null;
		}
		ODB odb = ODBFactory.open(Configuration.getDBFilename());
		IQuery query = new CriteriaQuery(Person.class, Where.equal("username",
				username));
		if (!odb.getObjects(query).isEmpty()) {
			Person p = (Person) odb.getObjects(query).getFirst();
			odb.close();
			return p;
		}
		odb.close();
		return null;
	}
	

	@Override
	public void reload(AbstractObject source) {
		Person sourcePerson  = (Person) source;
		this.username = sourcePerson.username;
		this.password = sourcePerson.password;
		this.firstname = sourcePerson.firstname;
		this.lastname = sourcePerson.lastname;
		this.birthday =sourcePerson.birthday;
		this.vibes =sourcePerson.vibes;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setVibes(ArrayList<Vibe> vibes) {
		this.vibes = vibes;
	}

	public ArrayList<Vibe> getVibes() {
		return vibes;
	}


	
	
	
}
